package com.example.com.transcribeeditpublish.views;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import com.example.com.transcribeeditpublish.R;
import com.example.com.transcribeeditpublish.myInterface.IOnBeautyParamsChangeListener;
import com.example.com.transcribeeditpublish.utils.VideoMaterialDownloadProgress;
import com.example.com.transcribeeditpublish.utils.beauty.BeautyParams;
import com.lcodecore.tkrefreshlayout.utils.DensityUtil;
import com.tencent.liteav.basic.log.TXCLog;


import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.GlidRadioTranform;

public class BeautySettingPannel extends FrameLayout implements SeekBar.OnSeekBarChangeListener {
    private final String TAG = "BeautySettingPannel";

    public static final int ITEM_TYPE_BEAUTY = 0;
    public static final int ITEM_TYPE_FILTTER = 1;

    private ArrayList<String> mSencodeGradleArrayString = new ArrayList<String>();
    private int[][] mSzSeekBarValue = null;
    public static final int BEAUTYPARAM_BEAUTY = 1;
    public static final int BEAUTYPARAM_FILTER = 5;

    private RadioGroup radioGroup;
    private RecyclerView recycle;
    private ArrayList<Integer> mFilterList;
    private int mCurrentSelectedPos;
    private View meiyanContaner;
    private SeekBar mopi;
    private TextView mopiNum;
    private SeekBar hongrun;
    private SeekBar meibai;
    private TextView hongrunNum;
    private TextView meibaiNum;
    private RadioButton mRfilter;

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int i = seekBar.getId();
        BeautyParams params = new BeautyParams();
        params.mBeautyStyle = BeautyParams.BEAUTYPARAM_BEAUTY_STYLE_SMOOTH;
        if (i == R.id.mopi) {
            mSzSeekBarValue[ITEM_TYPE_BEAUTY][0] = progress;
            mopiNum.setText(String.valueOf(progress));
            params.mBeautyLevel = progress;
        } else if (i == R.id.hongrun) {
            mSzSeekBarValue[ITEM_TYPE_BEAUTY][1] = progress;
            params.mRuddyLevel = progress;
            hongrunNum.setText(String.valueOf(progress));
        } else if (i == R.id.meibai) {
            params.mWhiteLevel = progress;
            mSzSeekBarValue[ITEM_TYPE_BEAUTY][2] = progress;
            meibaiNum.setText(String.valueOf(progress));
        }
        setBeautyStyle(params);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }


    private String[] mBeautyFilterTypeString = {
            "原画",
            "标准",    // 4
            "樱红",    // 8
            "云裳",    // 8
            "纯真",    // 8
            "白兰",    // 10
            "元气",    // 8
            "超脱",    // 10
            "香氛",    // 5
            "美白",    // 5
            "浪漫",    // 5
            "清新",    // 5
            "唯美",    // 5
            "粉嫩",    // 5
            "怀旧",    // 5
            "蓝调",    // 5
            "清凉",    // 5
            "日系",    // 5
    };


    private Context mContext;

    private IOnBeautyParamsChangeListener mBeautyChangeListener;

    public BeautySettingPannel(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
        initSeekBarValue();
        setListenter();
    }


    public void setBeautyParamsChangeListener(IOnBeautyParamsChangeListener listener) {
        mBeautyChangeListener = listener;
    }


    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.beauty_pannel, this);
        radioGroup = (RadioGroup) view.findViewById(R.id.beauty_group);
        mRfilter = (RadioButton) view.findViewById(R.id.r_filter);
        meiyanContaner = view.findViewById(R.id.meiyan_contaner);
        mopi = (SeekBar) view.findViewById(R.id.mopi);
        hongrun = (SeekBar) view.findViewById(R.id.hongrun);
        meibai = (SeekBar) view.findViewById(R.id.meibai);
        mopiNum = (TextView) view.findViewById(R.id.mopi_num);
        hongrunNum = (TextView) view.findViewById(R.id.hongrun_num);
        meibaiNum = (TextView) view.findViewById(R.id.meibai_num);
        recycle = (RecyclerView) view.findViewById(R.id.b_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mFilterList = new ArrayList<Integer>();
        mFilterList.add(R.mipmap.orginal);
        mFilterList.add(R.mipmap.biaozhun);
        mFilterList.add(R.mipmap.yinghong);
        mFilterList.add(R.mipmap.yunshang);
        mFilterList.add(R.mipmap.chunzhen);
        mFilterList.add(R.mipmap.bailan);
        mFilterList.add(R.mipmap.yuanqi);
        mFilterList.add(R.mipmap.chaotuo);
        mFilterList.add(R.mipmap.xiangfen);
        mFilterList.add(R.mipmap.langman);
        mFilterList.add(R.mipmap.qingxin);
        mFilterList.add(R.mipmap.weimei);
        mFilterList.add(R.mipmap.fennen);
        mFilterList.add(R.mipmap.huaijiu);
        mFilterList.add(R.mipmap.landiao);
        mFilterList.add(R.mipmap.qingliang);
        mFilterList.add(R.mipmap.rixi);
        ((SimpleItemAnimator) recycle.getItemAnimator()).setSupportsChangeAnimations(false);
        recycle.setAdapter(new BeauutyAdapter());
    }

    private void setListenter() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == mRfilter.getId()) {
                    recycle.setVisibility(VISIBLE);
                    meiyanContaner.setVisibility(GONE);
                } else {
                    recycle.setVisibility(GONE);
                    meiyanContaner.setVisibility(VISIBLE);
                }
            }
        });
        hongrun.setOnSeekBarChangeListener(this);
        meibai.setOnSeekBarChangeListener(this);
        mopi.setOnSeekBarChangeListener(this);

    }


    private Bitmap decodeResource(Resources resources, int id) {
        TypedValue value = new TypedValue();
        resources.openRawResource(id, value);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inTargetDensity = value.density;
        return BitmapFactory.decodeResource(resources, id, opts);
    }


    public Bitmap getFilterBitmapByIndex(int index) {
        Bitmap bmp = null;
        switch (index) {
            case 1:
                bmp = decodeResource(getResources(), R.drawable.filter_biaozhun);
                break;
            case 2:
                bmp = decodeResource(getResources(), R.drawable.filter_yinghong);
                break;
            case 3:
                bmp = decodeResource(getResources(), R.drawable.filter_yunshang);
                break;
            case 4:
                bmp = decodeResource(getResources(), R.drawable.filter_chunzhen);
                break;
            case 5:
                bmp = decodeResource(getResources(), R.drawable.filter_bailan);
                break;
            case 6:
                bmp = decodeResource(getResources(), R.drawable.filter_yuanqi);
                break;
            case 7:
                bmp = decodeResource(getResources(), R.drawable.filter_chaotuo);
                break;
            case 8:
                bmp = decodeResource(getResources(), R.drawable.filter_xiangfen);
                break;
            case 9:
                bmp = decodeResource(getResources(), R.drawable.filter_white);
                break;
            case 10:
                bmp = decodeResource(getResources(), R.drawable.filter_langman);
                break;
            case 11:
                bmp = decodeResource(getResources(), R.drawable.filter_qingxin);
                break;
            case 12:
                bmp = decodeResource(getResources(), R.drawable.filter_weimei);
                break;
            case 13:
                bmp = decodeResource(getResources(), R.drawable.filter_fennen);
                break;
            case 14:
                bmp = decodeResource(getResources(), R.drawable.filter_huaijiu);
                break;
            case 15:
                bmp = decodeResource(getResources(), R.drawable.filter_landiao);
                break;
            case 16:
                bmp = decodeResource(getResources(), R.drawable.filter_qingliang);
                break;
            case 17:
                bmp = decodeResource(getResources(), R.drawable.filter_rixi);
                break;
            default:
                bmp = null;
                break;
        }
        return bmp;
    }

    public int getFilterProgress(int index) {
        return mSzSeekBarValue[ITEM_TYPE_FILTTER][index];
    }

    //设置滤镜
    private void setFilter(int index) {
        Bitmap bmp = getFilterBitmapByIndex(index);
        if (mBeautyChangeListener != null) {
            BeautyParams params = new BeautyParams();
            params.mFilterBmp = bmp;
            params.filterIndex = index;
            mBeautyChangeListener.onBeautyParamsChange(params, BEAUTYPARAM_FILTER);
        }
    }


    public void setCurrentFilterIndex(int index) {
        RecyclerView.Adapter adapter = recycle.getAdapter();
        adapter.notifyItemChanged(mCurrentSelectedPos);
        mCurrentSelectedPos = index;
        adapter.notifyItemChanged(mCurrentSelectedPos);
    }


    // 设置美颜类型
    private void setBeautyStyle(BeautyParams params) {
        if (mBeautyChangeListener != null) {
            mBeautyChangeListener.onBeautyParamsChange(params, BEAUTYPARAM_BEAUTY);
        }
    }


    private void initSeekBarValue() {
        if (null == mSzSeekBarValue) {
            mSzSeekBarValue = new int[2][18];
            for (int i = 1; i < mSzSeekBarValue[ITEM_TYPE_FILTTER].length; i++) {
                mSzSeekBarValue[ITEM_TYPE_FILTTER][i] = 5; // 一般滤镜的推荐值
            }

            // 前八个滤镜的推荐值 （其他默认为5）
            mSzSeekBarValue[ITEM_TYPE_FILTTER][1] = 4;
            mSzSeekBarValue[ITEM_TYPE_FILTTER][2] = 8;
            mSzSeekBarValue[ITEM_TYPE_FILTTER][3] = 8;
            mSzSeekBarValue[ITEM_TYPE_FILTTER][4] = 8;
            mSzSeekBarValue[ITEM_TYPE_FILTTER][5] = 10;
            mSzSeekBarValue[ITEM_TYPE_FILTTER][6] = 8;
            mSzSeekBarValue[ITEM_TYPE_FILTTER][7] = 10;
            mSzSeekBarValue[ITEM_TYPE_FILTTER][8] = 5;


            for (int i = 0; i < mSzSeekBarValue[ITEM_TYPE_BEAUTY].length; i++) {
                if (i >= mSencodeGradleArrayString.size()) {
                    break;
                }
                switch (mSencodeGradleArrayString.get(i)) {
                    case "美颜":
                        mSzSeekBarValue[ITEM_TYPE_BEAUTY][i] = 5;
                        break;
                    case "美白":
                        mSzSeekBarValue[ITEM_TYPE_BEAUTY][i] = 5;
                        break;
                    case "红润":
                        mSzSeekBarValue[ITEM_TYPE_BEAUTY][i] = 5;
                        break;
                    default:
                        break;
                }
            }
        }
    }


    public String[] getBeautyFilterArr() {
        return mBeautyFilterTypeString;
    }

    private class BeauutyAdapter extends RecyclerView.Adapter<BHolder> {
        @Override
        public BHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.filter_layout, parent, false);
            return new BHolder(view);
        }

        @Override
        public void onBindViewHolder(BHolder holder, final int position) {

            Glide.with(mContext).load(mFilterList.get(position)).transform(new GlidRadioTranform(mContext)).into(holder.ivImage);
            Glide.with(mContext).load(R.mipmap.filter_selected).transform(new GlidRadioTranform(mContext)).into(holder.ivImageTint);

            if (mCurrentSelectedPos == position) {
                holder.ivImageTint.setVisibility(View.VISIBLE);
            } else {
                holder.ivImageTint.setVisibility(View.GONE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setCurrentSelectedPos(position);
                }
            });
        }

        public void setCurrentSelectedPos(int pos) {
            int tPos = mCurrentSelectedPos;
            mCurrentSelectedPos = pos;
            this.notifyItemChanged(tPos);
            this.notifyItemChanged(mCurrentSelectedPos);
            setFilter(mCurrentSelectedPos);
        }

        @Override
        public int getItemCount() {
            return mFilterList.size();
        }
    }

    private class BHolder extends RecyclerView.ViewHolder {
        public final ImageView ivImage;
        public final ImageView ivImageTint;

        public BHolder(View view) {
            super(view);
            ivImage = (ImageView) itemView.findViewById(R.id.filter_image);
            ivImageTint = (ImageView) itemView.findViewById(R.id.filter_image_tint);
        }
    }
}
